# Makefile fortement inspiré de 
# "Recursive Make Considered Harmful" 
# -- http://miller.emu.id.au/pmiller/books/rmch/


#CC := gcc
CFLAGS := -Wall -Wextra -g
RM := rm
RMFLAGS := -f
TARGET := rt_ciai
MODULES := reseau main messages interface mere erreur utils ChaineCond
# look for include files in each of the modules
CFLAGS += $(patsubst %,-I%, $(MODULES))
# extra libraries if required
LIBS := -lrt -lpthread -lm
# each module will add to this
SRC :=
# include the description for each module
include $(patsubst %, %/module.mk, $(MODULES))
# determine the object files
OBJ := $(patsubst %.c, %.o, $(filter %.c, $(SRC)))
# link the program
$(TARGET): $(OBJ)
	$(CC) $(CFLAGS) -o $@ $(OBJ) $(LIBS)
# calculate C include dependencies
%.d: %.c
	./depend.sh `dirname $*.c` $(CFLAGS) $*.c > $@	
# include the C include dependencies
include $(OBJ:.o=.d)

# Compile the C files
%.o: %.c
	$(CC) $(CFLAGS) -o $@ -c $<

# clean the build directories
clean:
	$(RM) $(RMFLAGS) $(TARGET) test $(OBJ) $(patsubst %.o, %.d, $(OBJ)) $(TEST_OBJ) $(patsubst %.o, %.d, $(TEST_OBJ))

# Framework de test
TEST_MODULES := tests/messages tests/main tests/reseau tests/chaine_cond
# look for include files in each of the modules
TEST_CFLAGS += $(patsubst %,-I%, $(TEST_MODULES))
# extra test libraries if required
TEST_LIBS := -lcheck
TEST_LIBS += $(LIBS)
# each test module will add to this
TEST_SRC :=
# include the description for each module
include $(patsubst %, %/module.mk, $(TEST_MODULES))
# determine the object files
TEST_OBJ := $(patsubst %.c, %.o, $(filter %.c, $(TEST_SRC)))
# add all objects except for the one containing the main function
TEST_OBJ += $(filter-out main/main.o, $(OBJ))
# link the test program
test: $(TEST_OBJ)
	$(CC) $(CFLAGS) -o $@ $(TEST_OBJ) $(TEST_LIBS)
# include the C include dependencies
include $(TEST_OBJ:.o=.d)

# build the documentation
doc:
	doxygen Doxyfile
